package com.wenhuang.chatgptopen.controller;

import com.wenhuang.chatgptopen.mapper.FansMapper;
import com.wenhuang.chatgptopen.pojo.Fans;
import com.wenhuang.chatgptopen.pojo.User;
import com.wenhuang.chatgptopen.retu.SearchReturn;
import com.wenhuang.chatgptopen.service.FansService;
import com.wenhuang.chatgptopen.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 *@Time：2023/6/10
 *@Author：Jelly
 */
@Controller
public class FansController {

    @Autowired
    private UserService userService;
    @Autowired
    private FansService fansService;

    private User user;

    private SearchReturn searchReturn;

    //添加粉丝
    @PostMapping("/addFans")
    @ResponseBody
    public String addFans(Fans fans) {
        System.out.println(fans);
        user = new User();

        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date);
        fans.setCreateTime(time);

        String message = "fail";
        if((fansService.addFans(fans))!=null) {
            //添加成功
            user.setId(fans.getFansId());
            user = userService.getUserById(user);
            userService.changFans(user);
            message = "success";
        }
        return message;
    }

    //删除粉丝
    @PostMapping("/deleteFans")
    @ResponseBody
    public String deleteFans(Fans fans) {

        user = new User();
        String message = "fail";
        if((fansService.deleteFans(fans))!=null) {
            //删除成功成功
            user.setId(fans.getFansId());
            user = userService.getUserById(user);
            System.out.println(user);
            userService.deleteFans(user);
            message = "success";
        }
        return message;
    }

    //搜索用户功能
    @PostMapping("/searchUser")
    @ResponseBody
    public SearchReturn searchUser(User user, HttpServletResponse response) {

        searchReturn = new SearchReturn();

        if(user != null) {
            if((userService.searchUser(user))!= null) {
                //有返回用户
                searchReturn.setResult("success");
                searchReturn.setList(userService.searchUser(user));
            }else {
                searchReturn.setResult("fail");
            }
        }else {
            response.setStatus(401);
        }
        return searchReturn;
    }

}
